package com.glory.hikvisionmobile.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;


import androidx.fragment.app.FragmentManager;

import com.glory.hikvisionmobile.base.App;
import com.glory.hikvisionmobile.base.BaseActivity;
import com.glory.hikvisionmobile.bean.TrackIn.LotInfoBean;
import com.glory.hikvisionmobile.bean.net.BaseResponseBean;
import com.glory.hikvisionmobile.utils.DialogUtils;
import com.glory.hikvisionmobile.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils;

public class DialogActivity extends BaseActivity {

    private FreeUI_GeneralFragmentDialog lotDialog;
    private FreeUI_GeneralFragmentDialog workDialog;
    private FreeUI_GeneralFragmentDialog tipDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutResId() {
        return 0;
    }

    @Override
    protected void initView() {
        final String extra = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        if (extra.contains("exchange")) {

            lotDialog = DialogUtils.getScanDialog(this, "请扫描批次号", s -> {
                requestDataForNet(s);
            });

            lotDialog.setCallBack(this::finish);

            lotDialog.show(getSupportFragmentManager(),"lotScan");
        }else if (extra.contains("check")){
            workDialog = FreeApi_DialogUtils.getScanDialog(this, "请扫描设备编号", s -> {
                Intent intent = new Intent(DialogActivity.this,Work_TrackInActivity.class);
                intent.putExtra("workNum",s);
                startActivity(intent);
                workDialog.dismiss();
            });

            workDialog.setCallBack(this::finish);
            workDialog.show(getSupportFragmentManager(),"workScan");
        }
    }

    private void requestDataForNet(final String lot) {
        JsonObject map = new JsonObject();
        map.addProperty("actionType","Check");
        map.addProperty("lotId",lot);
        NetUtils.requestDara("HIK.PdaGetLotState",this,map,new FreeUI_DialogEntityCallBack<BaseResponseBean<LotInfoBean>>(
                new TypeToken<BaseResponseBean<LotInfoBean>>() {
                }.getType(), getSupportFragmentManager(), this){

            @Override
            public void onSuccess(Response<BaseResponseBean<LotInfoBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())) {
                    LotInfoBean body = response.body().getBody();
                    startTrackIn(body, lot);

                }
            }

            @Override
            public void onError(Response<BaseResponseBean<LotInfoBean>> response) {
                super.onError(response);
                loadError(response.getException(),"HIK.PdaGetLotState");
            }
        });

    }

    private void startTrackIn(LotInfoBean lotInfoBean,String s) {
        if (lotInfoBean.getHoldState().equals("On")){
            showFailDialog("当前状态为hold",getSupportFragmentManager());
            lotDialog.dismiss();
            return;
        }
                //进站

        if (lotInfoBean.getState().equals("WAIT")){
            switch (lotInfoBean.getOperationType()){
                case "DefaultTrackIn":
                case "DieSortingTrackIn":
                case "DieBoundTrackIn":
                    if (actionControl("Work_TrackInActivity","PDAWorkFragment","无进站权限"))
                        return;
                    startActivity(Work_TrackInActivity.class,lotInfoBean,s);
                    lotDialog.dismiss();
                    break;
                case "GraphiteBoatTrackIn":
                    if (actionControl("Work_GraphiteBoatTrackInActivity","PDAWorkFragment","无石墨舟进站权限"))
                        return;
                    startActivity(Work_GraphiteBoatTrackInActivity.class,lotInfoBean,s);
                    lotDialog.dismiss();
                    break;
                case "Al5TeosEtchTrackIn":
                    if (actionControl("Work_Al5TeosEtchTrackInActivity","PDAWorkFragment","无反刻进站权限"))
                        return;
                    startActivity(Work_Al5TeosEtchTrackInActivity.class,lotInfoBean,s);
                    lotDialog.dismiss();
                    break;
                default:
                    showFailDialog("客制化进站，PDA暂时无法处理",getSupportFragmentManager());
            }

            //出站

        }else if (lotInfoBean.getState().equals("RUN")) {

            switch (lotInfoBean.getOperationType()) {
                case "DefaultTrackOut":
                    if (actionControl("Work_TrackOutActivity","PDAWorkFragment","无普通出站权限"))
                        return;
                    startActivity(Work_TrackOutActivity.class, lotInfoBean, s);
                    lotDialog.dismiss();
                    break;
                case "GraphiteBoatTrackOut":
                    if (actionControl("Work_GraphiteBoatTrackOutActivity","PDAWorkFragment","无石墨舟出站权限"))
                        return;
                    startActivity(Work_GraphiteBoatTrackOutActivity.class, lotInfoBean, s);
                    lotDialog.dismiss();
                    break;
                case "DieSortingTrackOut":
                    if (actionControl("Work_SortingTrackOutActivity","PDAWorkFragment","无分选出站权限"))
                        return;
                    startActivity(Work_SortingTrackOutActivity.class,lotInfoBean,s);
                    lotDialog.dismiss();
                    break;
                case "DieBoundTrackOut":
                    if (actionControl("Work_PatchTrackOutActivity","PDAWorkFragment","无贴片出站权限"))
                        return;
                    startActivity(Work_PatchTrackOutActivity.class,lotInfoBean,s);
                    lotDialog.dismiss();
                    break;
                case "SolderPreweldingTrackOut":
                    if (actionControl("Work_SorlderPreweldingTrackOutActivity","PDAWorkFragment","无贴片出站权限")) {
                        return;
                    }
                    startActivity(Work_SorlderPreweldingTrackOutActivity.class,lotInfoBean,s);
                    lotDialog.dismiss();
                    break;
                case "VacuumSealingTrackOut":
                    if (actionControl("Work_VacuumSealingTrackOutActivity","PDAWorkFragment","无贴片出站权限")) {
                        return;
                    }
                    startActivity(Work_VacuumSealingTrackOutActivity.class,lotInfoBean,s);
                    lotDialog.dismiss();
                    break;
                case "WuLiaoTouLiaoTrackOut":
                    if (actionControl("Work_MaterialFeedingActivity","PDAWorkFragment","无出站权限")) {
                        return;
                    }
                    startActivity(Work_MaterialFeedingActivity.class,lotInfoBean,s);
                    lotDialog.dismiss();
                    break;
                default:
                    showFailDialog("客制化出站，PDA暂时无法处理",getSupportFragmentManager());
            }


        }else {
            showFailDialog("当前批次状态为" + lotInfoBean.getState() + ",无法进站",getSupportFragmentManager());
        }

    }


    private void startActivity(Class<? extends Activity> activity, LotInfoBean data, String lotNum){
        Bundle bundle = new Bundle();
        bundle.putString("lotType",data.getOperationType());
        bundle.putBoolean("isRequireEqp",data.isRequireEqp());
        bundle.putString("lotNum",lotNum);
        FreeApi_Utils.skipWithExtraIntent("header",bundle,DialogActivity.this,activity);
    }


    @Override
    protected void initData() {

    }

    private void showFailDialog(String message, FragmentManager fragmentManager)
    {
        tipDialog = FreeApi_DialogUtils.getConfirmDialog(App.app, "确定", message, () -> {
            lotDialog.dismiss();
        });
        tipDialog.show(fragmentManager,"DIALOG");
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return null;
    }
}
